<?php

/* @var $this \yii\web\View */

use app\assets\ShareAsset;

ShareAsset::register($this);
?>
<script type="text/x-template" id="share-dialog-template">
    <div>
        <!--MOB SHARE BEGIN-->
        <div class="-mob-share-ui -mob-share-ui-theme -mob-share-ui-theme-slide-bottom"
             style="display: none">
            <ul class="-mob-share-list">
                <li class="-mob-share-weibo"><p>新浪微博</p></li>
                <li class="-mob-share-qzone"><p>QQ空间</p></li>
                <li class="-mob-share-qq"><p>QQ好友</p></li>
                <li class="-mob-share-weixin"><p>微信</p></li>
                <li class="-mob-share-douban"><p>豆瓣</p></li>
                <li class="-mob-share-facebook"><p>Facebook</p></li>
                <li class="-mob-share-twitter"><p>Twitter</p></li>
                <li class="-mob-share-pocket"><p>Pocket</p></li>
                <li class="-mob-share-google"><p>Google+</p></li>
                <li class="-mob-share-youdao"><p>有道云笔记</p></li>
                <li class="-mob-share-tumblr"><p>Tumblr</p></li>
                <li class="-mob-share-instapaper"><p>Instapaper</p></li>
                <li class="-mob-share-linkedin"><p>LinkedIn</p></li>
            </ul>
            <div class="-mob-share-close">取消</div>
        </div>
        <div class="-mob-share-ui-bg"></div>
    </div>
</script>
<script>
    Vue.prototype.$is_weixin = function () {
        var ua = window.navigator.userAgent.toLowerCase();
        return ua.match(/MicroMessenger/i) == 'micromessenger';
    };

    Vue.prototype.$share_url = function (url, title, pic) {
        mobShare.config({
            debug: true, // 开启调试，将在浏览器的控制台输出调试信息
            appkey: '270f597a62372', // appkey
            params: {
                url: url, // 分享链接
                title: title, // 分享标题
                description: '', // 分享内容
                pic: pic, // 分享图片，使用逗号,隔开
                reason: '',//只应用与QZone与朋友网
            },
            callback: function (plat, params) {
                console.log(params);
            }
        });

        mobShare.ui.init();
        mobShare.ui.open();
    }

    Vue.prototype.$share_webpage = function (pic, url, title, desc) {
        let is_weixin_res = this.$is_weixin();
        console.log(is_weixin_res);
        if (is_weixin_res) {
            window.wx.updateAppMessageShareData({
                title: title, // 分享标题
                link: url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                imgUrl: pic, // 分享图标
                desc: desc,
                success: function (r) {
                }
            });

            window.wx.updateTimelineShareData({
                title: title, // 分享标题
                link: url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                imgUrl: pic, // 分享图标
                desc: desc,
                success: function () {
                }
            });
        }


        this.$share_url(
            url,
            title,
            pic,
            desc
        );
    };

    Vue.prototype.$fix_url_by_weixin = function () {
        let is_weixin_res = this.$is_weixin();
        if (is_weixin_res) {
            let promise = new Promise(function (resolve, reject) {
                axios.get('/indexapi/wx/url_info',
                    {
                        params: {
                            url: encodeURIComponent(window.location.href)
                        }
                    }).then(function (response) {
                    let res = response.data;
                    window.wx.config(res.data)
                    window.wx.ready(() => {
                        resolve();
                    });
                });
            });
        }

        return is_weixin_res;
    };

    Vue.prototype.$download_share = function () {
        (function (d, s, id) {
            let js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) {
                return;
            }
            js = d.createElement(s);
            js.id = id;
            js.src = "https://f1.webshare.mob.com/code/mob-share.js?appkey=270f597a62372";
            fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', '-mob-share'));
        (function (d, s, id) {
            let css, fcss = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) {
                return;
            }
            css = d.createElement(s);
            css.id = id;
            css.href = "https://f1.webshare.mob.com/code/mob-share.css";
            css.rel = "stylesheet";
            fcss.parentNode.insertBefore(css, fcss);
        }(document, 'link', '-mob-share-css'));
    }


    // 定义一个名为 share-dialog 的新组件
    Vue.component('share-dialog', {
        template: '#share-dialog-template'
    })
</script>
